package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.dal;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.database.DBHelper;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.User;

public class UserInfoDAL {
    private final DBHelper dbHelper;
    private static final String tableName = "UserInfo";

    public UserInfoDAL(Context context) {
        dbHelper = new DBHelper(context, false);
    }

    public long addUser(User newUser) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", newUser.getUsername());
        values.put("password", newUser.getPassword());
        values.put("email", newUser.getEmail());
        values.put("role", newUser.getRole());

        try {
            return db.insertOrThrow(tableName, null, values);
        } catch (SQLiteConstraintException e) {
            return -1;
        } finally {
            db.close();
        }
    }

    public User login(String username, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] columns = {"userId", "email", "role"};
        String selection = "username = ? AND password = ?";
        String[] selectionArgs = {username, password};

        Cursor cursor;
        try {
            cursor = db.query(
                    tableName,    // 表名
                    columns,       // 要查询的列
                    selection,     // WHERE 条件
                    selectionArgs, // 参数绑定
                    null,          // groupBy
                    null,          // having
                    null           // orderBy
            );

            if (cursor != null && cursor.moveToFirst()) {
                int userId = cursor.getInt(cursor.getColumnIndexOrThrow("userId"));
                String email = cursor.getString(cursor.getColumnIndexOrThrow("email"));
                int role = cursor.getInt(cursor.getColumnIndexOrThrow("role"));
                cursor.close();

                return new User(userId, email, role);
            }

            return null;
        } finally {
            db.close();
        }
    }

    public User getUserInfoById(Integer userId) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] columns = {"username", "password", "email", "role"};
        String selection = "userId = ?";
        String[] selectionArgs = {userId.toString()};

        Cursor cursor;
        try {
            cursor = db.query(
                    tableName,    // 表名
                    columns,       // 要查询的列
                    selection,     // WHERE 条件
                    selectionArgs, // 参数绑定
                    null,          // groupBy
                    null,          // having
                    null           // orderBy
            );

            if (cursor != null && cursor.moveToFirst()) {
                String username = cursor.getString(cursor.getColumnIndexOrThrow("username"));
                String password = cursor.getString(cursor.getColumnIndexOrThrow("password"));
                String email = cursor.getString(cursor.getColumnIndexOrThrow("email"));
                Integer role = cursor.getInt(cursor.getColumnIndexOrThrow("role"));
                cursor.close();
                return new User(userId, username, password, email, role);
            }

            return new User(userId, "username", "password", "email", 1);
        } finally {
            db.close();
        }
    }

    public long updateUserInfo(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", user.getUsername());
        values.put("password", user.getPassword());
        values.put("email", user.getEmail());

        try {
            return db.update(tableName, values, "userId = ?", new String[]{user.getUserId().toString()});
        } catch (SQLiteConstraintException e) {
            return -1;
        } finally {
            db.close();
        }
    }
}
